package MetaApp;

import java.util.*;

public class Two {

    public static void main(String[] args) {
        int[] arr = new int[]{3,6,9,1,2,6};
        System.out.println(answer(arr));
    }

    public static int[] answer(int[] x) {
        int one = getOne(x);
        int[] two = getTwo(one);
        int[] three = getThree(two);
        return three;
    }

    private static int[] getThree(int[] array) {
        Set<Integer> set = new TreeSet<>();
        for (int i : array) {
            set.add(i);
        }
        int[] result = new int[set.size()*3];
        int index = 0;
        for (Integer i : set) {
            result[index++] = i;
            result[index++] = i;
            result[index++] = i;
        }
        return result;
    }

    private static int[] getTwo(int n) {
        List<Integer> list = new ArrayList<>();
        n = n<0 ? -n : n;
        for(int i=1; i<=n; i++) {
            if(n%i==0) {
                list.add(i);
            }
        }
        if (list.size()>2) {
            return new int[]{list.get(list.size()-1), list.get(list.size()-2)};
        } else if (list.size()==2){
            return new int[]{list.get(0), list.get(1)};
        } else if (list.size()==1) {
            return new int[]{list.get(0)};
        } else {
            return new int[]{};
        }
    }

    private static int getOne(int[] array) {
        int len = array.length;
        if (len<2)  return 0;
        Arrays.sort(array);
        int[] newArray;
        if (len>9) {
            newArray = new int[9];
            System.arraycopy(array, 0, newArray, 0, 9);
        } else {
            newArray = array;
        }
        int result = 0;
        for (int i = 1; i < newArray.length; i++) {
            int temp = Math.abs(newArray[i]-newArray[i-1]);
            if (temp>result) {
                result = temp;
            }
        }
        return result;
    }
}
